<?php


namespace App\Libraries\Warehouse\Strategies;


use App\Libraries\Warehouse\Contracts\DockingBehavior;
use App\Libraries\Warehouse\StrategyAbstract;
use App\Models\Order;
use Exception;
use Illuminate\Database\Eloquent\Model;

class CreateOrder extends StrategyAbstract implements DockingBehavior
{

    /**
     * @var Model
     * @author luffyzhao@vip.126.com
     */
    protected $data;

    /**
     * @param $data Model
     * @throws Exception
     * @author luffyzhao@vip.126.com
     */
    public function __construct($data)
    {
        if (!($data instanceof Order)) {
            throw new Exception('参数不正确');
        }
        $data->loadMissing(['client', 'buyer', 'products', 'platform', 'deliver']);
        $this->data = $data;
    }



    /**
     * @return array
     * @throws Exception
     * @author luffyzhao@vip.126.com
     */
    public function map(): array
    {
        return [
            'HeaderRequest' => $this->headerRequest($this->data->client),
            'OrderInfo' => $this->orderInfo($this->data)
        ];
    }

    /**
     * 订单消息体
     * @param Order $data
     * @return array
     * @author luffyzhao@vip.126.com
     */
    protected function orderInfo(Order $data){
        return [
            'orderModel' => '0',
            'warehouseCode' => 'CSBL',
            'toWarehouseCode' => 'CSBL',
            'oabCounty' => 'CN',
            'oabStateName' => $data->buyer->province,
            'oabCity' => $data->buyer->city,
            'oabDistrict' => $data->buyer->district,
            'smCode' => $data->deliver->code,
            'orderStatus' => 4,
            'referenceNo' => $data->reference_no,
            'oabName' => $data->buyer->receiving_name,
            'oabStreetAddress1' => $data->buyer->address,
            'oabPhone' => $data->buyer->receiving_telephone,
            'buyerName' => $data->buyer->name,
            'buyerPhone' =>  $data->buyer->telephone,
            'currencyCode' => 'RMB',
            'idType' => $data->buyer->id_type,
            'idNumber' => $data->buyer->id_number,
            'orderProduct' => $this->products($data->products),
            'ebcCode' => $data->client->ebc_code,
            'ebcName' => $data->client->ebc_name,
            'ebpCode' => $data->platform->ebp_code,
            'ebpName' => $data->platform->ebp_name,
        ];
    }

    /**
     * @param $data
     * @return array
     * @author luffyzhao@vip.126.com
     */
    protected function products($data)
    {
        $array = [];
        foreach ($data as $value) {
            $array[] = [
                'productSku' => $value->sku,
                'opQuantity' => $value->quantity,
                'dealPrice' => $value->price,
                'transactionPrice' => $value->total_price,
            ];
        }
        return $array;
    }

    /**
     * 获取 url
     * @return string
     * @author luffyzhao@vip.126.com
     */
    public function getUrl(): string
    {
        return 'http://hnoms.bestlucklog.com/default/order-soap/wsdl';
    }

    /**
     * @return string
     * @author luffyzhao@vip.126.com
     */
    public function getCallName(){
        return 'createOrder';
    }
}